package com.wjm.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.wjm.annotation.LoginValidate;
import com.wjm.entity.Admin;
import com.wjm.service.impl.AdminServiceImpl;
import com.wjm.util.StringUtil;

@Controller
@RequestMapping("/web/admin")
@SessionAttributes({ "admin" })
public class AdminController {

	@Resource(name = "adminService")
	private AdminServiceImpl adminService;

	private final String msgKey = "msg";
	private final String statusKey = "status";
	private final String listKey = "list";

	@RequestMapping("/login")
	@LoginValidate(needLogin = false)
	public String login(String username, String password, Model model) {
		String result = "/web/admin/login";
		try {
			if (username != null && !username.equals("") && password != null && !password.equals("")) {
				Admin admin = adminService.login(username, password);
				if (admin != null) {
					model.addAttribute(StringUtil.ADMIN_KEY, admin);
					result = "redirect:/web/admin";
				} else {
					model.addAttribute(msgKey, "账号或者密码不正确！");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "/error";
		}
		return result;
	}

	@RequestMapping("/logout")
	public String logout(@ModelAttribute(StringUtil.ADMIN_KEY) Admin admin) {
		String result = "redirect:/web/admin/login";
		return result;
	}

	@RequestMapping("")
	public String admin(String res) {
		String result = "/web/admin/" + res;
		if (res == null || res.equals("")) {
			result = "/web/admin/admin";
		}
		return result;
	}

	@RequestMapping("/change")
	@ResponseBody
	public String change(@ModelAttribute(StringUtil.ADMIN_KEY) Admin admin, String oldPass, String newPass) {
		String result = "{\"result\":\"fail\"}";
		try {
			if (admin.getAdminPass().equals(oldPass)) {
				Admin newAdmin = new Admin();
				newAdmin.setAdminId(admin.getAdminId());
				newAdmin.setAdminPass(newPass);
				if (adminService.update(newAdmin)) {
					admin.setAdminPass(newPass);
					result = "{\"result\":\"success\"}";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/query")
	public String query(@ModelAttribute(StringUtil.ADMIN_KEY) Admin admin, int status, Model model) {
		String result = "/web/admin/admin-show";
		try {
			List<Admin> list = adminService.getByStatus(status);
			if (status == 1 || status <= 0) {
				for (int i = 0; i < list.size(); i++) {

					Admin ad = list.get(i);

					if (admin.getAdminId() == ad.getAdminId()) {
						list.remove(i);
						break;
					}
				}
			}
			model.addAttribute(statusKey, status);
			model.addAttribute(listKey, list);
		} catch (Exception e) {
			e.printStackTrace();
			result = "error";
		}
		return result;
	}

	@RequestMapping("/updateStatus")
	@ResponseBody
	public String update(int adminId, int status) {
		String result = "{\"result\":\"fail\"}";
		try {
			if (adminService.updateStatus(adminId, status)) {
				result = "{\"result\":\"success\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/add")
	@ResponseBody
	public String add(Admin admin) {
		String result = "{\"result\":\"fail\"}";
		try {
			if (admin != null && admin.getAdminCode() != null && !admin.getAdminCode().equals("")
					&& admin.getAdminName() != null && !admin.getAdminName().equals("") && admin.getAdminPass() != null
					&& !admin.getAdminPass().equals("") && adminService.save(admin)) {
				result = "{\"result\":\"success\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
